//
//  ObserverLineViewController.m
//  Rail
//
//  Created by hychen on 2018/3/2.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "ObserverLineViewController.h"
#import "ObserverLineTableViewCell.h"
#import "ObserverCameraController.h"

#import "ESPictureBrowser.h"

@interface ObserverLineViewController () <ESPictureBrowserDelegate>
@property (nonatomic, strong) ESPictureBrowser *browser;
@property (nonatomic, strong) NSMutableArray *imageUrlArray;
@end

@implementation ObserverLineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem addSimpleTitleView:@"巡线管理"];
    
    [self.navigationItem addRightNavItem:self title:@"巡线拍照" selector:@selector(gotoObserverCamera)];
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)refreshData {
    self.currentPage = 1;
    self.isOpenFooter = NO;
    NSString *pageString = [NSString stringWithFormat:@"%ld",self.currentPage];
    BMWeakSelf
    [HttpRequest loadPatrolLinePhotoListDataWithPage:pageString success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]]) {
            weakSelf.dataArray = [NSMutableArray arrayWithArray:object];
            [weakSelf.tableView reloadData];
            if (object.count > 9) {
                weakSelf.isOpenFooter = YES;
            }
            [weakSelf.tableView.mj_footer resetNoMoreData];
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}

- (void)loadMoreData {
    self.currentPage ++;
    NSString *pageString = [NSString stringWithFormat:@"%ld",self.currentPage];
    BMWeakSelf
    [HttpRequest loadPatrolLinePhotoListDataWithPage:pageString success:^(id object) {
        if ([object isKindOfClass:[NSArray class]]) {
            [weakSelf.dataArray addObjectsFromArray:object];
        }
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_footer endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}

- (void)removePatrolItemWithIndexPath:(NSIndexPath *)indexPath {
    PatrolLineModel *model = [self.dataArray objectAtIndex:indexPath.row];
    Show_Msg(@"删除中")
    BMWeakSelf
    [HttpRequest deletePatrolLineDataWithSeqId:model.seqid success:^(id object) {
        Dismiss_NoMsg
        //删除数据，和删除动画
        [weakSelf.dataArray removeObjectAtIndex:indexPath.row];
        [weakSelf.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
    }];
}

- (void)gotoObserverCamera {
    //巡线拍照
    ObserverCameraController *vc = [ObserverCameraController new];
    BMWeakSelf
    [vc setBlockCameraSuccess:^{
        [weakSelf.tableView.mj_header beginRefreshing];
    }];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PatrolLineModel *model = [self.dataArray objectAtIndex:indexPath.row];
    ObserverLineTableViewCell *cell = [ObserverLineTableViewCell cellWithTableView:tableView class:[ObserverLineTableViewCell class]];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    PatrolLineModel *model = [self.dataArray objectAtIndexSafe:indexPath.row];
    if ([model isKindOfClass:[PatrolLineModel class]]) {
        return 70.f;
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //跳转
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //直接查看照片 UI - 24查看照片
    PatrolLineModel *model = [self.dataArray objectAtIndexSafe:indexPath.row];
    self.imageUrlArray = [NSMutableArray array];
    if ([model.snapshot_url1 objectIsNotEmpty]) {
        [self.imageUrlArray addObject:model.snapshot_url1];
    }
    if ([model.snapshot_url2 objectIsNotEmpty]) {
        [self.imageUrlArray addObject:model.snapshot_url2];
    }
    if ([model.snapshot_url3 objectIsNotEmpty]) {
        [self.imageUrlArray addObject:model.snapshot_url3];
    }
    
    //图片浏览器
    NSString *title = model.device_name;
    if (![title isKindOfClass:[NSString class]] || [title isEqualToString:@""]) {
        title = model.area_name?:@"";
    }
    
    NSMutableArray *contentArray = [NSMutableArray array];
    NSMutableArray *titleArray = [NSMutableArray array];
    
    for (int i = 0; i < self.imageUrlArray.count; ++i) {
        [titleArray addObject:title];
        if ([model.note objectIsNotEmpty]) {
            [contentArray addObject:model.note?:@""];
        }
        else if ([model.content objectIsNotEmpty]) {
            [contentArray addObject:model.content?:@""];
        }
    }
    
    self.browser = [[ESPictureBrowser alloc] initWithTitles:titleArray contents:contentArray];
    self.browser.delegate = self;
    [self.browser showFromView:self.view picturesCount:self.imageUrlArray.count currentPictureIndex:0];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

//设置进入编辑状态时，Cell不会缩进
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

//点击删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //在这里实现删除操作
    [self removePatrolItemWithIndexPath:indexPath];
}

#pragma mark - 图片浏览器
- (NSString *)pictureView:(ESPictureBrowser *)pictureBrowser highQualityUrlStringForIndex:(NSInteger)index {
    return [self.imageUrlArray objectAtIndexSafe:index];
}
@end
